package cn.springcloud.fix.quartz.controller;

import cn.springcloud.fix.common.core.base.BaseResponse;
import cn.springcloud.fix.common.core.base.Result;
import cn.springcloud.fix.common.core.utils.SpringUtil;
import cn.springcloud.fix.common.log.annotation.AsyncLog;
import cn.springcloud.fix.quartz.api.request.CreateJobRequest;
import cn.springcloud.fix.quartz.api.request.ListJobRequest;
import cn.springcloud.fix.quartz.api.request.TriggerJobRequest;
import cn.springcloud.fix.quartz.api.request.UpdateJobRequest;
import cn.springcloud.fix.quartz.api.response.ListJobResponse;
import cn.springcloud.fix.quartz.config.QuartzManager;
import cn.springcloud.fix.quartz.service.ScheduleService;
import com.alibaba.fastjson.JSON;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @PostMapping("/getJobs")
    public ListJobResponse getJobs(@Validated @RequestBody ListJobRequest request) {
        return scheduleService.getJobs(request);
    }

    /**
     * 创建job
     */
    @PostMapping("/createJob")
    public BaseResponse createJob(@Validated @RequestBody CreateJobRequest request) {
        return scheduleService.createJob(request);
    }

    /**
     * 立即触发job
     * localhost:8084/quartz/schedule/triggerJob
     */
    @PostMapping("/triggerJob")
    public BaseResponse triggerJob(@Validated @RequestBody TriggerJobRequest request) {
        return scheduleService.triggerJob(request);
    }

    /**
     * 更新job
     * localhost:8084/quartz/schedule/updateJob
     */
    @PostMapping("/updateJob")
    public BaseResponse updateJob(@Validated @RequestBody UpdateJobRequest request) {
        return scheduleService.updateJob(request);
    }

    /**
     * 删除job
     */
    @PostMapping("/deleteJob/{id}")
    public BaseResponse deleteJob(@PathVariable("id") String id) {
        return scheduleService.deleteJob(id);
    }

    /**
     * 暂停job
     * localhost:8084/quartz/schedule/pauseJob/createJobTask
     */
    @PostMapping("/pauseJob/{id}")
    public BaseResponse pauseJob(@PathVariable("id") String id) {
        return scheduleService.pauseJob(id);
    }

    /**
     * 恢复job
     * localhost:8084/quartz/schedule/resumeJob/createJobTask
     */
    @PostMapping("/resumeJob/{id}")
    public BaseResponse resumeJob(@PathVariable("id") String id) {
        return scheduleService.resumeJob(id);
    }
}
